package com.xxkj.drugtraceability.ui.departhd

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.base.BaseVMFragment
import com.xxkj.drugtraceability.bean.DrugTrace
import com.xxkj.drugtraceability.databinding.FragmentDepartBinding
import com.xxkj.drugtraceability.dialog.BlueDialog
import org.koin.androidx.viewmodel.ext.android.sharedViewModel

/**
 * author: zhoujingjin
 * created on: 2024/12/3 11:39
 * description:
 */
class DepartDrugHdFragment:BaseVMFragment<FragmentDepartBinding>(R.layout.fragment_depart) {
    val viewModel by sharedViewModel<DepartDrugHdCollectionViewModel>()
    private lateinit var adapter: DepartDrugHdCollectionAdapter
    var type = 0
    companion object {
        fun newInstance(type:Int): DepartDrugHdFragment {
            val fragment = DepartDrugHdFragment()
            fragment.arguments = Bundle().apply {
                putInt("type",type)
            }
            return fragment
        }
    }
    override fun initView() {
        binding.apply {
            recyclerView.layoutManager = LinearLayoutManager(context)
            arguments?.let {
                type = it.getInt("type",0)
            }
            adapter = DepartDrugHdCollectionAdapter(type,{
              val data1 =  viewModel.unDataList.value
                data1?.let {list->
                    val item  = list.removeAt(it)
                    list.add(item)
                    viewModel.unDataList.postValue(list)
                }
            },{
               var data1 = viewModel.unDataList.value
               var data2 = viewModel.dataList.value
                if (data2?.contains(it)==false){
                    data2.add(0,it)
                }
                data1?.remove(it)
                viewModel.unDataList.postValue(data1)
                viewModel.dataList.postValue(data2)
            })
            recyclerView.adapter = adapter
        }
    }

    override fun initData() {

    }

    override fun startObserve() {
        viewModel.unDataList.observe(this, Observer {
            adapter.setCanScan(!viewModel.isHd)
            if (type == 0) {
                if (it.isNullOrEmpty()) {
                    adapter.setList(mutableListOf())
                    adapter.setEmptyView(R.layout.layout_nodata)
                } else {
                    adapter.setList(it)
                }
            }

        })
//        viewModel.selectIndex.observe(this, Observer {
//            if (type==0){
//                adapter.data.forEachIndexed { index, drugInfoBean ->
//                    drugInfoBean.isSelected = it == index
//                    if (viewModel.barcode.isNotEmpty()) {
//                        drugInfoBean.drugTracInfos?.forEach { drug ->
//                            if (drug.drugTracCodg == viewModel.barcode) {
//                                BlueDialog(requireContext(), "该追溯码已在列表中，无需重复扫码", type = 1) {}
//                                return@Observer
//                            }
//                        }
//                        if (it == index && drugInfoBean.drugTracInfos?.size == drugInfoBean.fysl?.toDouble()
//                                ?.toInt()
//                        ) {
//                            BlueDialog(requireContext(), "该药品溯源码添加超过上限", type = 1) {}
//                            return@Observer
//                        }
//                    }
//                }
//                if (it!=-1){
//                    binding.recyclerView.scrollToPosition(it)
//                    adapter.data[it].isSelected = true
//                    if (viewModel.barcode.isNotEmpty()){
//                        if(adapter.data[it].drugTracInfos==null){
//                            adapter.data[it].drugTracInfos = mutableListOf()
//                        }
//                        adapter.data[it].drugTracInfos!!.add(DrugTrace(drugTracCodg = viewModel.barcode))
//                    }
//                    if (adapter.data[it].drugTracInfos!!.size==adapter.data[it].fysl?.toDouble()?.toInt()){
//                        //需要将它移到已扫码里面
//                        viewModel.dataList.value?.add(adapter.data[it])
//                        viewModel.dataList.postValue(viewModel.dataList.value)
//                        adapter.data.removeAt(it)
//                        viewModel.unDataList.value =adapter.data
//                    }
//                }
//
//                adapter.notifyDataSetChanged()
//                if (viewModel.lastIndex!=-1)
//                    adapter.notifyItemChanged(viewModel.lastIndex)
//            }
//        })
                viewModel.dataList.observe(this, Observer {
                    adapter.setCanScan(!viewModel.isHd)
                    if (type==1){
                        if (it.isNullOrEmpty()) {
                            adapter.setList(mutableListOf())
                            adapter.setEmptyView(R.layout.layout_nodata)
                        } else {
                            adapter.setList(it)
                        }
                    }
                })
    }
}